package com.yylany.service.impl;

import com.google.common.collect.Lists;
import com.yylany.mapper.UserMapper;
import com.yylany.pojo.User;
import com.yylany.pojo.vo.UserVO;
import com.yylany.service.FileService;
import com.yylany.service.MailService;
import com.yylany.service.UserService;
import com.yylany.utils.BeanHelper;
import com.yylany.utils.HashUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * User  yyl
 * Description: house
 * Created by yaoyonglong on 2018/11/3 23:37
 */
@Service
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;

    @Autowired
    private FileService fileService;

    @Autowired
    private MailService mailService;

    @Value("${file.prefix}")
    private String imgPrefix;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean addAccount(UserVO user) {

        user.setPasswd(HashUtils.encryPassword(user.getPasswd()));
        List<String> imglist = fileService.getImgPaths(Lists.newArrayList(user.getAvatarFile()));

        if (!imglist.isEmpty()) {
            user.setAvatar(imglist.get(0));
        }
        User account = new User();
        BeanUtils.copyProperties(user, account);
        account.setType(1);
        account.setEnable(2);
        account.setAgencyId(1);
        account.setCreateTime(new Date());
        userMapper.insert(account);

        mailService.registerNotify(account.getEmail());
        return true;
    }

    @Override
    public boolean enable(String key) {
        return mailService.enable(key);
    }

    @Override
    public User auth(String email, String password) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email", email);
        criteria.andEqualTo("passwd", HashUtils.encryPassword(password));
//        criteria.andEqualTo("passwd", password);
        criteria.andEqualTo("enable", 1);

        List<User> users = userMapper.selectByExample(example);

        if (users != null && users.get(0) != null) {
            users.forEach(u -> {
                u.setAvatar(imgPrefix + u.getAvatar());
            });
            return users.get(0);
        }
        return null;
    }

    @Override
    public void updateUser(User updateUser, String email) {

        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email", email);
        User user = userMapper.selectOneByExample(example);
        BeanHelper.onUpdate(updateUser);
        updateUser.setId(user.getId());
        userMapper.updateByPrimaryKeySelective(updateUser);
    }

    @Override
    public User getUserByEmail(String email) {
        Example example = new Example(User.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("email", email);
        return userMapper.selectOneByExample(example);
    }


}
